﻿using QInfrastructure.Interfaces.SystemManager;
using QInfrastructure.Models;
using QInfrastructure.Models.SystemManager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QInfrastructure.Services.SystemManager
{
	public class SystemUserThemeService : QBaseMainService, TSystemUserTheme
	{
		public SystemUserTheme GetSystemUserThemeById(Guid userId)
		{
			using var db = CreateContext();
			var info=db.Set<SystemUserTheme>().FirstOrDefault(x => x.UserId == userId);
			if (info == null)
			{
				info = new SystemUserTheme
				{
					ThemeName="default"
				};
			}
			return info;
		}
		public void AddOrUpdateSystemUserTheme(SystemUserTheme theme)
		{
			
			using var db = CreateContext();
			var exit=db.Set<SystemUserTheme>().FirstOrDefault(x=>x.UserId == theme.UserId);
			if (exit != null)
			{
				exit.ThemeName= theme.ThemeName;
			}
			else
			{
				theme.Id=Guid.NewGuid();
				db.Add(theme);
			}
			db.SaveChanges();
			QInfrastructureCache.RemoveQCache(typeof(SystemUserTheme).FullName);
		}
	}
}
